import argparse
import re

# 统计字符数
def count_characters(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        text = file.read()
        return len(text)
    
# 统计单词数
def count_words(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        text = file.read()
        words = re.findall(r'\b\w+\b', text)
        return len(words)
    
# 统计句数
def count_sentences(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        text = file.read()
        sentences = re.split(r'[\.\?\!]\s', text)
        return len(sentences)

# 统计代码行、空行、注释行
def count_code_lines(file_path):
    code_lines = 0
    empty_lines = 0
    comment_lines = 0
    with open(file_path, 'r', encoding='utf-8') as file:
        lines = file.readlines()
        for line in lines:
            line = line.strip()
            # 空行
            if line == '':
                empty_lines += 1

            # 注释行
            elif line.startswith('#'):  # Python注释以#开头
                # C中头文件以#include开头，防止错计
                if not line.startswith('#include'):     
                    comment_lines += 1   
            elif line.startswith('//'):  # C++、Java等注释以//开头
                comment_lines += 1
            elif line.startswith('/*') and line.endswith('*/'):  # C、C++多行注释以/*开头，*/结尾
                comment_lines += 1
            elif line.startswith('/*'):  # C、C++多行注释以/*开头
                comment_lines += 1
                while not line.endswith('*/'):  # 如果没有以*/结尾，则继续读取下一行
                    comment_lines += 1
                    line = file.readline().strip()
            elif line.startswith('<!--') and line.endswith('-->'):  # HTML注释以<!--开头，-->结尾
                comment_lines += 1

            # 代码行
            else:
                code_lines += 1
                
    return code_lines, empty_lines, comment_lines
    
